<template>
    <uni-popup ref="privacy" :type="type" :is-mask-click="true">
        <template v-if="type == 'bottom'">
            <view class="d-kuan d-beijing-bai d-dibu-0 d-yuanjiao-shang-10">
                <view class="d-kuan d-wenzi-20 d-neikuang-10 ">{{ privacyContractName }}</view>
                <view class="d-neikuang-10 " style="font-size:15px;padding:10px;text-align: justify; color: #626262;">
					在您使用前请仔细阅读,
                    <text @click="openxy" style="color: blue;">{{ privacyContractName }}</text>
                    如果您同意，请点击“同意”继续使用
				</view>
                <view class="d-xia-10" style="border-top-style:solid;border-top-color:#d8c9c945;border-top-width:1px;padding: 10px;">
                    <view style="width:30%;float:left;margin-left: 15px;">
                        <button type="default" style="border-radius:50px;" plain="true" size="default" @click="doRefuse">不同意</button>
                    </view>
                    <view style="width:50%;float:right;margin-right: 15px;">
                        <button type="default" size="default" style="width: 180px;border-radius:50px;background-color: #51b68f;color: #ffffff;" id="agree-btn" open-type="agreePrivacyAuthorization"
                                @agreeprivacyauthorization="AgreePrivacyAuthorization">同意</button>
                    </view>
					<view class="d-clear"></view>
                </view>
				<view class="d-clear"></view>
            </view>
			<view class="d-clear"></view>
        </template>
        <template v-if="type == 'dialog'">
            <view style="text-align:center;background-color:#fff;border-radius:4%;height:200px;width:85%;margin:0 auto;">
                <view style="font-size:18px;font-weight: bold;padding:10px;border-bottom-style:solid;border-bottom-color: #d8c9c945;border-bottom-width:1px;">{{ privacyContractName }}</view>
                <view style="font-size:15px;padding:10px;text-align: justify;">在您使用前请仔细阅读,
                    <text @click="openxy" style="color: blue;" >{{ privacyContractName }}</text>
                    如果您同意，请点击“同意”继续使用</view>
                <view style="border-top-style:solid;border-top-color:#d8c9c945;border-top-width:1px;padding: 10px;">
                    <view style="float:left;margin-left: 15px;">
                        <button type="warn" size="mini" @click="doRefuse">不同意</button>
                    </view>
                    <view style="float:right;margin-right: 15px;">
                        <button type="primary" size="mini" style="width: 150px;" id="agree-btn" open-type="agreePrivacyAuthorization"
                                @agreeprivacyauthorization="AgreePrivacyAuthorization">同意</button>
                    </view>
                </view>
            </view>
        </template>
    </uni-popup>
</template>

<script>
export default {
    name: "privacy",
    props: {
        show: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: 'dialog'
        },
        changeFocus:Function,
        refuse:Function,
        agree:Function,
    },
    data() {
        return {
            privacyContractName: "",
            webName: "",
            account: {}
        };
    },
    watch:{
        show: {
            handler: function(newVal) {
                if (newVal){
                    this.$refs.privacy.open()
                }
            },
            immediate: true
        },
    },
    mounted(){
        let _this = this
		//微信用户隐私接口
        if (wx.getPrivacySetting){
            wx.getPrivacySetting({
                success: (res) => {
                    //console.log(res)
                    _this.privacyContractName = res.privacyContractName
                    // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
                },
                fail: () => {},
                complete:() => {}
            })
        }
		//微信小程序新的用户协议

        if (wx.onNeedPrivacyAuthorization) {
            wx.onNeedPrivacyAuthorization(resolve => {
                console.log(resolve)
                // 需要用户同意隐私授权时
                // 弹出开发者自定义的隐私授权弹窗
                _this.$refs.privacy.open()
                _this.resolve = resolve

            })
        }

    },
    methods:{
        open(){
            this.$refs.privacy.open()
        },
        close(){
            this.$refs.privacy.close()
        },
        openxy(){
            wx.openPrivacyContract({
                success: (res) => {
                    console.log('success')
                }, // 打开成功
                fail: (res) => {
                    console.log(res)
                }, // 打开失败
                complete: () => {}
            })
        },
        AgreePrivacyAuthorization(){
            if (typeof this.agree=== 'function') {
                return this.agree()
            }
            if (typeof this.resolve=== 'function') {
                this.resolve({buttonId: 'agree-btn', event: 'agree'})
            }
            if (typeof this.changeFocus=== 'function') {
                this.changeFocus(true)
            }
            this.$refs.privacy.close()
        },
        doRefuse(){
			this.$refs.privacy.close()
            if (typeof this.refuse === 'function') {
                return this.refuse()
            }
            
        },
    }
}
</script>


